﻿using System.Text;

namespace System
{
    /// <summary>
    /// Base64帮助类
    /// </summary>
    public static class Base64Help
    {
        /// <summary>
        /// 字符串转Base64
        /// </summary>
        /// <param name="value">需要转换的内容</param>
        /// <param name="encoding">编码，转换时的编号格式,默认系统编码格式</param>
        /// <returns>转换后的Base64内容</returns>
        public static string StringToBase64(this string value, Encoding encoding = default)
        {
            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }

            byte[] bytes = encoding.GetBytes(value);
            return Convert.ToBase64String(bytes, 0, bytes.Length);
        }

        /// <summary>
        /// 将Base64还原为字符串
        /// </summary>
        /// <param name="value">需要转换的Base64</param>
        /// <param name="encoding">编码，转换时的编号格式,默认系统编码格式</param>
        /// <returns>转换后的字符串内容</returns>
        public static string Base64ToString(this string value, Encoding encoding = default)
        {
            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }
            byte[] bytes = Convert.FromBase64String(value);
            return encoding.GetString(bytes);
        }
    }
}